Added colors
authorJeroen van der Heijden <jeroen@transceptor.technology>
Sat, 6 Oct 2018 08:49:55 +0000 (10:49 +0200)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Sat, 6 Oct 2018 08:49:55 +0000 (10:49 +0200)
itest/testing/__init__.py
itest/testing/args.py
itest/testing/color.py [new file with mode: 0644]
itest/testing/spinner.py [new file with mode: 0644]
itest/testing/task.py [new file with mode: 0644]
test/test_version/test_version.c

index 282c8ff67c367f8dce02dc24a8c599a7c7bbd6bf..9ce56c322f355ba52bc75134257a83a0f77ebe4f 100644 (file)
@@ -1,7 +1,5 @@
-import sys
 import asyncio
 import logging
-import time
 from .client import Client
 from .client import InsertError
 from .client import PoolError
@@ -19,54 +17,7 @@ from .testbase import TestBase
 from .series import Series
 from .pipe_client import PipeClient as SiriDBAsyncUnixConnection
 from .args import parse_args
-
-SPINNER1 = \
-    ('▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▇', '▆', '▅', '▄', '▃', '▁')
-SPINNER2 = \
-    ('⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈')
-SPINNER3 = \
-    ('◐', '◓', '◑', '◒')
-
-
-class Spinner():
-
-    def __init__(self, charset=SPINNER3):
-        self._idx = 0
-        self._charset = charset
-        self._len = len(charset)
-
-    @property
-    def next(self):
-        char = self._charset[self._idx]
-        self._idx += 1
-        self._idx %= self._len
-        return char
-
-
-class Task():
-    def __init__(self, title):
-        self.running = True
-        self.task = asyncio.ensure_future(self.process())
-        self.success = False
-        self.title = title
-        self.start = time.time()
-
-    def stop(self, success):
-        self.running = False
-        self.success = success
-        self.duration = time.time() - self.start
-
-    async def process(self):
-        spinner = Spinner()
-        while self.running:
-            sys.stdout.write(f'{self.title:.<76}{spinner.next}\r')
-            sys.stdout.flush()
-            await asyncio.sleep(0.2)
-
-        if self.success:
-            print(f'{self.title:.<76}OK ({self.duration:.2f} seconds)')
-        else:
-            print(f'{self.title:.<76}FAILED ({self.duration:.2f} seconds)')
+from .task import Task
 
 
 async def _run_test(test, loglevel):
index 3a7506a2f0fc826ee0383b2b7d2333374f9f5411..da7171de7f3145408b76e046b9b8660f99c3c588 100644 (file)
@@ -1,5 +1,18 @@
+import os
+import subprocess
 import argparse
 from .server import Server
+from .color import Color
+
+
+def is_valgrind_installed():
+    with open(os.devnull, 'w') as fnull:
+        try:
+            subprocess.call(['valgrind'], stdout=fnull, stderr=fnull)
+        except OSError as e:
+            if e.errno == os.errno.ENOENT:
+                return False
+    return True
 
 
 def parse_args():
@@ -37,7 +50,17 @@ def parse_args():
 
     args = parser.parse_args()
 
-    Server.MEM_CHECK = args.mem_check
+    has_valgrind = is_valgrind_installed()
+
+    print("Test using valgrind for memory errors and leaks: ", end='')
+    if args.mem_check and not has_valgrind:
+        print(Color.warning('disabled (!! valgrind not found !!)'))
+    elif not args.mem_check:
+        print(Color.warning('disabled'))
+    else:
+        print(Color.success('enabled'))
+
+    Server.MEM_CHECK = args.mem_check and has_valgrind
     Server.HOLD_TERM = args.keep
     Server.TERMINAL = args.terminal
     Server.BUILDTYPE = args.build
diff --git a/itest/testing/color.py b/itest/testing/color.py
new file mode 100644 (file)
index 0000000..ff28fe2
--- /dev/null
@@ -0,0 +1,22 @@
+
+NORMAL = '\x1B[0m'
+RED = '\x1B[31m'
+GREEN = '\x1B[32m'
+YELLOW = '\x1B[33m'
+
+
+class Color:
+
+    @staticmethod
+    def success(text):
+        return f'{GREEN}{text}{NORMAL}'
+
+    @staticmethod
+    def warning(text):
+        return f'{YELLOW}{text}{NORMAL}'
+
+    @staticmethod
+    def error(text):
+        return f'{RED}{text}{NORMAL}'
+
+
diff --git a/itest/testing/spinner.py b/itest/testing/spinner.py
new file mode 100644 (file)
index 0000000..bf21fcc
--- /dev/null
@@ -0,0 +1,21 @@
+SPINNER1 = \
+    ('▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▇', '▆', '▅', '▄', '▃', '▁')
+SPINNER2 = \
+    ('⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈')
+SPINNER3 = \
+    ('◐', '◓', '◑', '◒')
+
+
+class Spinner():
+
+    def __init__(self, charset=SPINNER3):
+        self._idx = 0
+        self._charset = charset
+        self._len = len(charset)
+
+    @property
+    def next(self):
+        char = self._charset[self._idx]
+        self._idx += 1
+        self._idx %= self._len
+        return char
diff --git a/itest/testing/task.py b/itest/testing/task.py
new file mode 100644 (file)
index 0000000..7f26bd6
--- /dev/null
@@ -0,0 +1,36 @@
+import sys
+import time
+import asyncio
+from .spinner import Spinner
+from .color import Color
+
+
+class Task():
+    def __init__(self, title):
+        self.running = True
+        self.task = asyncio.ensure_future(self.process())
+        self.success = False
+        self.title = title
+        self.start = time.time()
+
+    def stop(self, success):
+        self.running = False
+        self.success = success
+        self.duration = time.time() - self.start
+
+    async def process(self):
+        spinner = Spinner()
+        while self.running:
+            sys.stdout.write(f'{self.title:.<76}{spinner.next}\r')
+            sys.stdout.flush()
+            await asyncio.sleep(0.2)
+
+        if self.success:
+            print(
+                f'{self.title:.<76}'
+                f'{Color.success("OK")} ({self.duration:.2f} seconds)')
+        else:
+            print(
+                f'{self.title:.<76}'
+                f'{Color.error("FAILED")} ({self.duration:.2f} seconds)')
+
index 95569e252a42b637400506979f69b16b010a659f..bfae981aae42b1a7fcf3dc207d41b5a8b10d3f07 100644 (file)
@@ -1,9 +1,9 @@
+#include <stdio.h>
+#include <stdlib.h>
 #include "../test.h"
 #include <siri/version.h>
 
 
-#include <stdio.h>
-#include <stdlib.h>
 
 int old_version_cmp(const char * version_a, const char * version_b)
 {
@@ -17,8 +17,6 @@ int old_version_cmp(const char * version_a, const char * version_b)
         a = strtol(str_a, &str_a, 10);
         b = strtol(str_b, &str_b, 10);
 
-        printf("%ld - %ld\n", a, b);
-
         if (a != b)
         {
             return a - b;